package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.vo.SpuSaveInfoVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: SpuController
 * @Author: WDD
 * @CreateTime: 2022-12-02  0
 * 。。。】9:18
 * @Description: TODO
 */
@Api(tags = "spu管理")
@RestController
@RequestMapping("/admin/product/")
public class SpuController {

    @Autowired
    SpuInfoService spuInfoService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @Autowired
    SpuImageService spuImageService;

    @ApiOperation("获取spu分页列表")
    @GetMapping("{page}/{limit}")
    public Result supPage(@PathVariable("page") Long page, @PathVariable("limit") Long limit,
                          @RequestParam("category3Id") Long category3Id){
        Page<SpuInfo> spuInfoPage = spuInfoService.lambdaQuery()
                .eq(SpuInfo::getCategory3Id, category3Id)
                .page(new Page<SpuInfo>(page, limit));
        return Result.ok(spuInfoPage);
    }

    @ApiOperation("保存spu")
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuSaveInfoVo spuSaveInfoVo){
        spuInfoService.saveSpuInfo(spuSaveInfoVo);
        return Result.ok();
    }

    @ApiOperation("根据spuId获取销售属性")
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrListBySpuId(@PathVariable("spuId") Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrService.spuSaleAttrListBySpuId(spuId);
        return Result.ok(spuSaleAttrList);
    }

    @ApiOperation("根据spuId获取图片列表")
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageListById(@PathVariable("spuId") Long spuId){
        List<SpuImage> spuImageList = spuImageService.lambdaQuery().eq(SpuImage::getSpuId, spuId).list();
        return Result.ok(spuImageList);
    }

}
